package com.infinit.easyreader.entity;

import android.os.Parcel;
import android.os.Parcelable;

import com.infinit.easyreader.utils.CommonUtils;

import java.util.Calendar;

/**
 * 类：Book [V 1.0.0]
 * 包含类 : com.infinit.easyreader.entity.Book
 * 作用：书籍的实体类
 * Created by rzy on 2016/1/30.
 */
public class Book implements Comparable<Book>, Parcelable {
    public static final Creator<Book> CREATOR = new Creator<Book>() {
        @Override
        public Book createFromParcel(Parcel in) {
            return new Book(in);
        }

        @Override
        public Book[] newArray(int size) {
            return new Book[size];
        }
    };
    /*
     * 书籍名
     */
    private String name;
    /*
     * 书籍路径
     */
    private String path;
    /*
    书籍封面
     */
    private String cover;
    /*
    是否为收藏书籍 1：真 0：假
     */
    private boolean isFavorite;
    /*
    上次阅读时间
     */
    private String lastReadTime = CommonUtils.format2String(Calendar.getInstance().getTime());

    //上次阅读位置
    /*
    父级目录在数据库中的ID
     */
    private int DirectoryId;
    private long lastReadPosition = 0;
    private int bookId;


    public Book() {
    }

    protected Book(Parcel in) {
        name = in.readString();
        path = in.readString();
        cover = in.readString();
        isFavorite = in.readByte() != 0;
        lastReadTime = in.readString();
        DirectoryId = in.readInt();
        lastReadPosition = in.readLong();
        bookId = in.readInt();
    }

    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public long getLastReadPosition() {
        return lastReadPosition;
    }

    public void setLastReadPosition(long lastReadPosition) {
        this.lastReadPosition = lastReadPosition;
    }

    public int getDirectoryId() {
        return DirectoryId;
    }

    public void setDirectoryId(int directoryId) {
        DirectoryId = directoryId;
    }

    public void setIsFavorite(boolean isFavorite) {
        this.isFavorite = isFavorite;
    }

    public String getLastReadTime() {
        return lastReadTime;
    }

    public void setLastReadTime(String lastReadTime) {
        if (lastReadTime == null) {
            this.lastReadTime = CommonUtils.format2String(Calendar.getInstance().getTime());
        } else {
            this.lastReadTime = lastReadTime;
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getCover() {
        return cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    public boolean isFavorite() {
        return isFavorite;
    }

    public void setisFavorite(boolean isFavorite) {
        this.isFavorite = isFavorite;
    }


    /**
     * 排序规则：在时间一致时，按照名称排序，否则按照时间进行排序
     *
     * @param another 书籍对象
     * @return 大于：1 小于：-1
     */

    @SuppressWarnings("NullableProblems")
    @Override
    public int compareTo(Book another) {

        if (another == null) {
            return 1;
        } else if (this.lastReadTime.equals(another.getLastReadTime())) {
            return this.getName().compareTo(another.getName());
        } else {
            //noinspection ConstantConditions
            return CommonUtils.parse2Date(this.lastReadTime).compareTo(CommonUtils.parse2Date(another.getLastReadTime()));


        }


    }


    @Override
    public boolean equals(Object o) {
        return o instanceof Book && ((Book) o).getName().equals(this.name);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(path);
        dest.writeString(cover);
        dest.writeByte((byte) (isFavorite ? 1 : 0));
        dest.writeString(lastReadTime);
        dest.writeInt(DirectoryId);
        dest.writeLong(lastReadPosition);
        dest.writeInt(bookId);
    }
}
